package com.book.manager.controller;

import cn.hutool.core.date.DateUtil;
import com.book.manager.entity.Users;
import com.book.manager.entity.msg;
import com.book.manager.service.MsgService;
import com.book.manager.util.R;
import com.book.manager.util.consts.Constants;
import com.book.manager.util.consts.ConvertUtil;
import com.book.manager.util.http.CodeEnum;
import com.book.manager.util.ro.PageIn;
import com.book.manager.util.vo.MsgOut;
import com.book.manager.util.vo.PageOut;
import com.book.manager.util.vo.UserOut;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api(tags = "消息管理")
@RestController
@RequestMapping("/msg")
public class MsgController {

    @Autowired
    MsgService msgService;
    @ApiOperation("消息列表")
    @PostMapping("/msglist")
    public R getMsg(@RequestBody PageIn pageIn) {
        if (pageIn == null) {
            return R.fail(CodeEnum.PARAM_ERROR);
        }
        // 封装分页出参对象
        PageInfo<msg> msgList = msgService.getMsgList(pageIn);
        PageOut pageOut = new PageOut();
        pageOut.setCurrPage(msgList.getPageNum());
        pageOut.setPageSize(msgList.getPageSize());
        pageOut.setTotal((int) msgList.getTotal());
        List<MsgOut> outs = new ArrayList<>();
        for (com.book.manager.entity.msg msg: msgList.getList()) {
            MsgOut out = new MsgOut();
            BeanUtils.copyProperties(msg,out);
            outs.add(out);
        }
        pageOut.setList(outs);
        return R.success(CodeEnum.SUCCESS,pageOut);
    }

    @ApiOperation("消息详情")
    @GetMapping("/msgdetail")
    public R MsgDetail(Integer msgid) {
        msg msg = msgService.findMsgById(msgid);
        if (msg!=null) {
           MsgOut out = new MsgOut();
            BeanUtils.copyProperties(msg,out);
            return R.success(CodeEnum.SUCCESS,out);
        }

        return R.fail(CodeEnum.NOT_FOUND);
    }

    @ApiOperation("编辑消息")
    @PostMapping("/updatemsg")
    public R modifyMsg(@RequestBody msg msg) {
        return R.success(CodeEnum.SUCCESS,msgService.updateMsg(msg));
    }

    @ApiOperation("删除消息")
    @GetMapping("/deleteMsg")
    public R delMsg(Integer msgid) {
        msgService.deleteMsg(msgid);
        return R.success(CodeEnum.SUCCESS);
    }
}
